<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/comment.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/dbConnection.php';

class CommentDao {
	private $_db;
	
	public function __construct() {
		$this->_db = new DbConnection();
	}
	
	public function addComment($comment) {	
		$query = sprintf("CALL sp_AddComment('%u','%u','%u','%s','%u', @CommentId)", 
			$comment->getUserID(), $comment->getInteractType(), $comment->getIndicteeID(),
			$comment->getMessage(), $comment->getCreatedDate());
			
		$result = $this->_db->executeQuery($query);
		$result = $this->_db->executeQuery("SELECT @CommentId");
		
		if (!$result) {
		    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return 0;
		}
		
		if ($row = mysql_fetch_assoc($result)) {						
			$id = $row['@CommentId'];			
		}		
		
		return $id;
	}

	public function getAllComments($indicteeID, $interactType) {
		$query = sprintf("SELECT * FROM comments WHERE IndicteeId = '%u' AND InteractType = '%u'",
					$indicteeID, $interactType);
							
		$result = $this->_db->executeQuery($query);
		
		if (!$result) {
		    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}
		
		$commentList = array();
		
		while($row = mysql_fetch_assoc($result)) {			
			$comment = new CommentModel();
			
			$comment->setCommentID($row['CommentId']);
			$comment->setCreatedDate($row['CreatedDate']);
			$comment->setIndicteeID($row['IndicteeId']);
			$comment->setInteractType($row['InteractType']);
			$comment->setMessage($row['Message']);
			$comment->setUserID($row['UserId']);
			$comment->setPositiveRate($row['PositiveRate']);
			$comment->setNegativeRate($row['NegativeRate']);
									
			$commentList[] = $comment;						
		}		
		
		mysql_free_result($result);
		
		return $commentList;
	}
	
	public function getComments($indicteeID, $interactType, $from, $to) {
		$query = sprintf("SELECT * FROM comments WHERE IndicteeId = '%u' AND InteractType = '%u' LIMIT %u, %u",
					$indicteeID, $interactType, $from, $to);
							
		$result = $this->_db->executeQuery($query);
		
		if (!$result) {
		    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}
		
		$commentList = array();
		
		while($row = mysql_fetch_assoc($result)) {			
			$comment = new CommentModel();
			
			$comment->setCommentID($row['CommentId']);
			$comment->setCreatedDate($row['CreatedDate']);
			$comment->setIndicteeID($row['IndicteeId']);
			$comment->setInteractType($row['InteractType']);
			$comment->setMessage($row['Message']);
			$comment->setUserID($row['UserId']);
			$comment->setPositiveRate($row['PositiveRate']);
			$comment->setNegativeRate($row['NegativeRate']);
									
			$commentList[] = $comment;			
		}		
		
		mysql_free_result($result);
		
		return $commentList;
	}
	
	public function getNumberOfComments($indicteeID, $interactType) {
		$query = sprintf("SELECT COUNT(*) FROM comments WHERE IndicteeId = '%u' AND InteractType = '%u'",
					$indicteeID, $interactType);
		$result = $this->_db->executeQuery($query);

		if (!$result) {
		    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}
		
		if ($row = mysql_fetch_row($result)) {						
			return $row[0];
		}		
	}

	public function updatePositiveRate($commentID) {
		$query = sprintf("UPDATE comments SET PositiveRate = PositiveRate + 1 WHERE CommentId = '%u'", $commentID);
		$result = $this->_db->executeQuery($query);
		if (!$result) {
		    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}
		return mysql_affected_rows();
	}
	
	public function updateNegativeRate($commentID) {
		$query = sprintf("UPDATE comments SET NegativeRate = NegativeRate + 1 WHERE CommentId = '%u'", $commentID);
		$result = $this->_db->executeQuery($query);
		if (!$result) {
		    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}
		return mysql_affected_rows();
	}
	
	public function getCommentByID($commentID) {
		$query = sprintf("SELECT * FROM comments WHERE CommentID = '%u'", $commentID);
							
		$result = $this->_db->executeQuery($query);
		
		if (!$result) {
		    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}
		
		if($row = mysql_fetch_assoc($result)) {			
			$comment = new CommentModel();
			
			$comment->setCommentID($row['CommentId']);
			$comment->setCreatedDate($row['CreatedDate']);
			$comment->setIndicteeID($row['IndicteeId']);
			$comment->setInteractType($row['InteractType']);
			$comment->setMessage($row['Message']);
			$comment->setUserID($row['UserId']);
			$comment->setPositiveRate($row['PositiveRate']);
			$comment->setNegativeRate($row['NegativeRate']);

			
		}		
		
		mysql_free_result($result);
		
		return $comment;
	}
}
?>